class Solution {
public:
    bool validateStackSequences(vector<int>& pu, vector<int>& po) {
        vector<int> v;
        int j=0;
        
        for(int i=0;i<pu.size();)
        {
            v.push_back(pu[i++]);
            while(!v.empty()&&j<po.size()&&v.back()==po[j])
            {
                v.pop_back();
                j++;
            }
        }
        if(!v.empty()) return false;
        return true;
    }
};
